﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using ConfigLibrary;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Excel;
using System.Threading;

namespace TridentReport
{
    public partial class bldgportal : Form
    {
        WebBrowser edds = new WebBrowser();
        WebBrowser observation = new WebBrowser();
        DataTable tridenttable;
        public static observationlog observationpage;
        public static HtmlElement eddspw;
        public static HtmlElement eddsemail;
        public static HtmlElement conf;
        public static HtmlElement eddsbutton;
        bool loggedin = false;  
        private Config eddsconfig;
        public static string password;
        public static string _username;
        private string _host;
        public static string _pass;
        private string _database;
        private int _port;
        int tr;        
        
        public bldgportal(DataTable trident, int i)
        {
            tr = i;
            tridenttable = trident;
            InitializeComponent();
            emailpassword();
            eddsload();                    
        }

        private void eddsload()
        {
            edds.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(edds_DocumentCompleted);
            edds.AllowNavigation = true;
            edds.Navigate("https://www.bldgportal.com/Login.aspx", false);
        }        

        void edds_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (loggedin == false)
            {
                eddsemail = edds.Document.GetElementById("UserNameTextBox");
                eddsemail.SetAttribute("value", _username); 
                eddspw = edds.Document.GetElementById("PasswordTextBox");
                eddspw.SetAttribute("value", _pass);
                conf = edds.Document.GetElementById("ConfCheckBox");
                conf.SetAttribute("checked", "checked");
                eddsbutton = edds.Document.GetElementById("LoginButton");
                eddsbutton.InvokeMember("click");
                loggedin = true;                
            }

            else if (loggedin == true)
            {
                observationpage = new observationlog(tridenttable, edds, tr);
                observationpage.ShowDialog();
                edds = null;
                observationpage = null;
                tridenttable = null;
                this.Close();
            }              
        }         

        public void emailpassword()
        {
            Stream s = File.Open(Properties.Settings.Default.EDDS, FileMode.Open);
            BinaryFormatter b = new BinaryFormatter();
            eddsconfig = (Config)b.Deserialize(s);
            s.Close();
            eddsconfig.GetSqlInfo(out _host, out _username, out _pass, out _database, out _port);
        }           
    }
}

